//Raj Akula
//CSC 362
//Program 5
//Selection Sort in ASM

#include<stdio.h>
#include<stdlib.h>

int main(){
	// setting the array
	int a[] = {123456, 342100, 87539, 606006, 443322, 198371, 99109, 88018, 707007};		
	int alength = 9;					// length of elements in array
	int alength1 = alength - 1;			// length of elements in array minus 1 so can be used in loop
	int min = 0;						// intilizing min value to be first element in the beg.
	int i = 0;							// outerloop counter
	int j = 0;							// innerloop counter
	int temp;							// temporary variable for swap
	int tempregister;					// temp register for array index temp
	int printcounter = 0;				// counter used for print
	int multiplier = 4;					// multiplier to help with array indexes in assymbely

	_asm{
outerloopcheck:		mov eax, i			// array index in eax
					cmp eax, alength1   // loop check for outer loop
					jge jumpout         // if all indexes in array are iterated to through jump out
					mov min, eax        // have minimum be the index you started with in every iteration
					add eax, 1			// getting iterator started for next iteration
					mov i, eax			// putting value back into variable so we can eax for soemthing else
					sub eax, 1			// undo the increment so eax can be used in the program
					mov ebx, i			// setting next iterator to use in the inner loop
					mov j, ebx			// then moving it into a variable so you can reuse ebx
innerloopcheck:		mov ebx, j			// getting ready for iterator
					cmp ebx, alength	// loop check for inner loop
					jge checkswap		// if we iterated through inner loop check if a swap is needed
					mov eax, j			// moving j into eax so it can be incremented
					add eax, 1			// incremnting to get ready for enxt loop
					mov j, eax			// moving it into variable so we can reuse eax
					mov eax, min		// setting min to eax so i can see the value in the array
					mul multiplier		// multiplying index by 4 so i can index it in the array
					mov ecx, a[eax]		// getting register to have value of the array
					mov eax, ebx		// moving ebx into eax so i can use multiplier
					mul multiplier		// multiply index by 4 so we can use get index in array
					mov edx, a[eax]		// setting register for compare
					cmp ecx, edx		// compare to see if there is a new minimum
					jl innerloopcheck	// if no new minimum then go to next iteration in loop
					mov min, ebx		// if there is a new minimum setting that value to min
					jmp innerloopcheck  // countine to next iteration
checkswap:			mov eax, i			// getting an array index to check with
					sub eax, 1			// initiliazing it to right value because before we incremented by one we are decrementing by one here
					mov ebx, min		// moving an index into register so we can compare 
					cmp eax, ebx		// compare values to see if there is a change in min value
					je outerloopcheck	// if there is not then continue with outer loop
					mul multiplier		// if there is then taking eax and multiplying by 4 to use for index
					mov ecx, a[eax]     // getting index
					mov tempregister, eax // tempregister so we can reuse eax
					mov eax, ebx		// moving ebx into eax so we can multiply it
					mul multiplier		// multiplying so we can use array index
					mov ebx, eax		// then moving it back into ebx
					mov eax, tempregister // moving tempregister back into eax
					mov edx, a[ebx]		//getting value of register
					mov temp, ecx		//putting value into temp so we can swap
					mov ecx, edx		// mov edx into ecx so performing swap
					mov edx, temp		// moving temp back into ebx
					mov a[eax], ecx		// putting back into array swapped
					mov a[ebx], edx		// putting back into array swapped
					jmp outerloopcheck	// reiterated through loop
jumpout:								// done with the loop
	}

	// Printing array
	printf("Array : {");					// print statement
	// looping through array
	while(printcounter < alength){
		// condition if to put a comma after array,
		if(printcounter < (alength-1))
			printf("%d, ", a[printcounter]); // printing each element
		// condition if last element, no comma and closed }
		else 
			printf("%d}", a[printcounter]);
		printcounter++;                  // incrementing thorugh loop
	}

	// just a scanf so we can read the output
	scanf("%d", printcounter);

}